/* 
拦截器
*/
import axios from "axios";
import eventEmitter from "./eventEmitter";

axios.defaults.headers.authorization = "token";

const successHandler = (response) => {
	console.log("successHandler", response);
	return response.data;
};

const errorHandler = (error) => {
	if (error.response.status === 401) {
		eventEmitter.emit("API:UN_AUTH");
	} else if (error.response.status === 400) {
		eventEmitter.emit("API:ERROR", error.response.data);
	}
};
// 请求拦截
axios.interceptors.request.use((config) => {
	console.log("request", config);
	return config;
});

// 响应拦截
axios.interceptors.response.use(successHandler, errorHandler);
